#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define rep(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii<=Ii##_;Ii++)
#define per(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii>=Ii##_;Ii--)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long double db;
#define fir first
#define sec second
#define siz(Aa) ((int)(Aa).size())
#define all(Aa) (Aa).begin(),(Aa).end()
#define ckmx(Aa,Bb) (Aa=max(Aa,Bb))
#define ckmn(Aa,Bb) (Aa=min(Aa,Bb))
#define int ll
const int N=5e5+10,inf=1e9+7,vinf=LLONG_MAX;
int n,m,w[N];
vector<int> a[N],c;
vector<int> mp[N<<1];
vector<array<int,3>> orz;
vector<pair<int,pair<int,int>>> ans;
void add(int x,int k,int b){
	orz.push_back({x,k,b});
}
void add(int l,int r,int k,int b){
	add(l,k,b);
	add(r+1,-k,-b);
}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);
	freopen("list.in","r",stdin);
	freopen("list.out","w",stdout);
	cin>>n;
	rep(i,1,n){
		int x;cin>>x;
		a[i].resize(x);
		for(int&j:a[i]) cin>>j;
		c.insert(c.end(),all(a[i]));
	}
	sort(all(c));
	c.erase(unique(all(c)),c.end());
	auto getc=[&](int x){return lower_bound(all(c),x)-c.begin();};
	cin>>m;
	rep(i,1,n) cin>>w[i];
	rep(i,1,n) if(!a[i].empty()){
		int x=siz(a[i]);
		add(1,inf,m,0);
		rep(j,0,x-2){
			int len=a[i][j+1]-a[i][j];
			add(1,len,m,0);
			add(len+1,inf,0,len*m);
			mp[getc(a[i][j+1])].emplace_back(len);
		}
		mp[getc(a[i][0])].emplace_back(inf);
	}
	rep(i,0,siz(c)-1){
		auto&j=mp[i];
		sort(all(j));
		int len=siz(j);
		int lst=1;
		rep(k,0,len-1){
			int cnt=len-k;
			add(lst,j[k],0,w[cnt]*cnt);
			lst=j[k]+1;
		}
	}
	sort(all(orz));
	rep(i,0,siz(orz)-1){
		if(i==i_ || orz[i][0]!=orz[i+1][0]){
			ans.emplace_back(orz[i][0],make_pair(orz[i][1],orz[i][2]));
		}else{
			orz[i+1][1]+=orz[i][1];
			orz[i+1][2]+=orz[i][2];
		}
	}
	int k=0,b=0;
	int mn=vinf;
	vector<int> pos;
	for(auto[i,j]:ans){
		if(i==inf+1) break;
		k+=j.fir;
		b+=j.sec;
		int now=k*i+b;
		ckmn(mn,now);
	}
	k=b=0;
	int ned=0;
	for(auto[i,j]:ans){
		if(ned){
			rep(_,ned,i-1) pos.emplace_back(_);
			ned=0;
		}
		if(i==inf+1) break;
		k+=j.fir;
		b+=j.sec;
		int now=k*i+b;
		if(mn==now){
			if(b==0){
				ned=i;
			}else{
				pos.emplace_back(i);
			}
		}
	}
	cout<<mn<<" "<<siz(pos)<<"\n";
	for(int i:pos) cout<<i<<" ";
	cout<<"\n";
return 0;}
/*
*/